Short Circuit Operators
Let's say we have two conditions: a == b and b == c. We want to execute some code only if both conditions are true, so we write (a == b) & (b == c). Now, from the computer's perspective, when it gets to this part of the code, there are two ways it could determine the truth of this conditional:
- Evaluate a, b, and c. Then make the necessary comparisons
- Evaluate a and b, see if they're equal. If so, evaluate c and complete the computation, otherwise stop right now.
& is called short circuit if it uses method 2. This is not important in the example we just gave, but consider what would happen if the conditions were a == SomeFunction() and b = SomeOtherFunction(). Then, in method 1, which is Salsa's method, both SomeFunction() and SomeOtherFunction() are evaluated, whereas with short circuit operators, if a didn't equal SomeFunction(), SomeOtherFunction() would never be called, which could have important side effects if it changed global variables, printed to the screen, etc..
Web page maintained by Jason Cohen